package com.gdk.ksoft.assembly.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.utils.extensions.ControlUtils;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * Created by hel on 2014-12-08.
 * 修改单价的提示框
 */
public class FixPriceDialog extends Dialog {

    public interface OnPriceChangedListener{
        public void onPriceChange(double price);
    }

    @ViewInject(R.id.dialog_fixPrice_etPrice)private EditText etPrice;//单价输入框
    private OnPriceChangedListener listener;//监听事件
    private double minPrice = 0;//最小价格
    private double realPrice = 0;//当前正常价格

    /**
     * 构造函数
     * @param context
     * @param realPrice 当前的价格
     * @param minPrice 最小价格
     * @param listener
     */
    public FixPriceDialog(Context context,double realPrice, final double minPrice,OnPriceChangedListener listener) {
        super(context,R.style.WindowDialogTheme);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View rootView = View.inflate(context, R.layout.dialog_fixprice,null);
        ViewUtils.inject(this, rootView);
        setContentView(rootView);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(lp);
        this.listener = listener;
        this.minPrice = minPrice;
        this.realPrice = realPrice;
        etPrice.setText(FormatUtils.exactNumber(realPrice,"0.00"));//设置原单价
    }

    @OnClick(value = {R.id.dialog_fixPrice_txtOk,R.id.dialog_fixPrice_txtCancel})
    public void OnViewClick(View view){
        switch(view.getId()){
            case R.id.dialog_fixPrice_txtOk://确定按钮
                String price = ControlUtils.getNumberStringFromTextView(etPrice);
                double finalPrice = Double.parseDouble(price);
                if(Double.compare(finalPrice,0) == 0){
                    if(listener!=null){
                        listener.onPriceChange(finalPrice);
                    }
                    this.dismiss();
                    return;
                }
                if(Double.compare(finalPrice,minPrice)<0){
                    Toast.makeText(getContext(), "请重新输入，您的单价小于预期值："+
                            FormatUtils.exactNumber(minPrice,"0.00")+"元!", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(listener!=null){
                    listener.onPriceChange(finalPrice);
                }
                this.dismiss();
                break;
            case R.id.dialog_fixPrice_txtCancel://取消按钮
                this.dismiss();
                break;
        }
    }

}
